package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangeng.entity.Article;
import com.sangeng.entity.vo.HotArticleVo;
import com.sangeng.mapper.ArticleMapper;
import com.sangeng.result.Result;
import com.sangeng.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.util.BeanCopyUtils;
import com.sangeng.util.Constants;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Bing
 * @since 2022-02-07
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    /**
     * 门户网站 - 查询最热门文章
     *
     * @return
     */
    @Override
    public Result hotArticleList() {
        final List<Article> articles = baseMapper.selectList(new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, Constants.ARTICLE_STATUS_NORMAL)
                .orderByDesc(Article::getViewCount)
                .last("limit 10"));
        // 使用封装的工具类拷贝对象，只返回需要显示的字段给前端
        final List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);

        return Result.okResult(articleVos);
    }
}
